package main

import "fmt"

func DeferClosureLoopV1() {
	for i := 0; i < 10; i++ {
		defer func() {
			fmt.Printf("i 的地址是 %p, 值是 %d \n", &i, i)
		}()
	}
}

func DeferClosureLoopV2() {
	for i := 0; i < 10; i++ {
		defer func(val int) {
			fmt.Printf("val 的地址是 %p, 值是 %d \n", &val, val)
		}(i)
	}
}

func DeferClosureLoopV3() {
	for i := 0; i < 10; i++ {
		j := i
		defer func() {
			fmt.Printf("j 的地址是 %p, 值是 %d \n", &j, j)
		}()
	}
}
func DeferReturn() int {
	a := 0
	defer func() {
		a = 1
	}()
	return a
}
func DeferReturnV1() (a int) {
	a = 0
	defer func() {
		a = 1
	}()
	return a
}
func DeferReturnV2() *MyStruct {
	a := &MyStruct{
		name: "Tom",
	}
	defer func() {
		a.name = "Jerry"
	}()
	return a
}

type MyStruct struct {
	name string
}
